/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.view;

import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import org.jvnet.substance.SubstanceLookAndFeel;
import org.model.Biblioteca;
import org.model.Livro;
import org.model.Pessoa;

/**
 *
 * @author Jack
 */
public class LivrosEmprestados extends javax.swing.JFrame {
private Biblioteca biblioteca;

    /**
     * Creates new form NovaBiblioteca
     */
    public LivrosEmprestados(Biblioteca biblioteca) {
        
        this.biblioteca = biblioteca;
        initComponents();
        PreencheTabela(biblioteca);
        jLabel3.setText(biblioteca.getNome());
//        initkeys();
        
    }

    public Biblioteca getBiblioteca() {
        return biblioteca;
    }

    public void setBiblioteca(Biblioteca biblioteca) {
        this.biblioteca = biblioteca;
    }

    

    
/*
 private void initkeys() {
     
       this.jTextFieldNovaBibli.addKeyListener(new KeyAdapter() {
            public void keyPressed(final KeyEvent e) {
                int key = e.getKeyCode();
                if (key == KeyEvent.VK_ENTER) {
                   btnSalvar.doClick();
                }
            }
        });
       
      //TECLA DE ATALHO Salvar(F3)
        Action actionTeclaF3 = new AbstractAction() {

    
            public void actionPerformed(ActionEvent arg0) {
                 btnSalvar.doClick();
            }
        };
       
             Action actionTeclaESC = new AbstractAction(){
           @Override
           public void actionPerformed(ActionEvent evt){
               dispose();
           }
       };
             
         //Variáveis de captura de teclas
        KeyStroke keyStrokeFechar = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        KeyStroke keyStrokeF2 = KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0);
        KeyStroke keyStrokeF3 = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0);  
        KeyStroke keyStrokeF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0);  
        KeyStroke keyStrokeF5 = KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0);  
        KeyStroke keyStrokeF6 = KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0); 
         

        InputMap inMap = btnSalvar.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inMap.put(keyStrokeF2, "Adicionar");
        inMap.put(keyStrokeF3, "Salvar");
        inMap.put(keyStrokeF4, "Cancelar");
        inMap.put(keyStrokeF5, "Modificar");
        inMap.put(keyStrokeF6, "Excluir");
        inMap.put(keyStrokeFechar, "Caralho");
        
        ActionMap amap = btnSalvar.getActionMap();
      //  amap.put("Adicionar", actionTeclaF2);
        amap.put("Salvar", actionTeclaF3);
       // amap.put("Cancelar", actionTeclaF4);
      //  amap.put("Modificar", actionTeclaF5);
     //   amap.put("Excluir", actionTeclaF6);
        amap.put("Caralho", actionTeclaESC);
        
             
             
       
 }

 
    */

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        btnSalvar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableLivrosEmprestados = new javax.swing.JTable();
        btnSalvar1 = new javax.swing.JButton();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/gravar.png"))); // NOI18N
        btnSalvar.setText("Salvar (F3)");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Livros da biblioteca :");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("x");

        jTableLivrosEmprestados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Cod", "Titulo", "Autor", "Emprestado"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTableLivrosEmprestados);

        btnSalvar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Excluir.png"))); // NOI18N
        btnSalvar1.setText("Remover");
        btnSalvar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvar1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(29, 29, 29)
                                .addComponent(jLabel3))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(131, 131, 131)
                        .addComponent(btnSalvar)
                        .addGap(46, 46, 46)
                        .addComponent(btnSalvar1)))
                .addContainerGap(62, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalvar)
                    .addComponent(btnSalvar1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed

    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnSalvar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvar1ActionPerformed
        
        if (jTableLivrosEmprestados.getModel().getRowCount() > 0) {
        int linhaSelecionada = jTableLivrosEmprestados.getSelectedRow();
            if (linhaSelecionada >= 0) {
                int resp = JOptionPane.showConfirmDialog(this, "Deseja realemente Devolver este Emprestimo",
                        "confirmação", JOptionPane.YES_NO_OPTION);
                if (resp == JOptionPane.YES_NO_OPTION) {
                    String cod = jTableLivrosEmprestados.getModel().getValueAt(linhaSelecionada, 0).toString();
                       
                   ArrayList<Livro> ListaLivro = biblioteca.getLivrosIndisponiveis();
                    for (Livro umLivro : ListaLivro) {

                        if (umLivro.getCodigo() == Integer.parseInt(cod)) {
                            biblioteca.devolucao(umLivro);
                           
                            PreencheTabela(biblioteca);
                           
                            break;
                        }

                    }

                }

            } else {
                JOptionPane.showMessageDialog(null, "Selecione Uma Linha");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Faça Uma pesquisa");
        }
        
    }//GEN-LAST:event_btnSalvar1ActionPerformed


//    Menu Principal não é necessário 
    public static void main(String args[]) {
          
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                try {
                    javax.swing.UIManager.setLookAndFeel(new SubstanceLookAndFeel());
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(LivrosEmprestados.class.getName()).log(Level.SEVERE, null, ex);
                }
                    break;
                }
            }
        
 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ArrayList<Livro> teste = null;
               
               
                //new LivrosEmprestados(teste).setVisible(true);
            }
        });
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalvar;
    private javax.swing.JButton btnSalvar1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableLivrosEmprestados;
    // End of variables declaration//GEN-END:variables

    private void PreencheTabela(Biblioteca biblioteca) {
       
        String Status;
        DefaultTableModel model = (DefaultTableModel) this.jTableLivrosEmprestados.getModel();
        
        model.setNumRows(0);
      
        ArrayList<Livro> ListaLivro = biblioteca.getLivrosIndisponiveis();
        
        for(Livro Umlivro : ListaLivro){
        Pessoa pessoa = Umlivro.getPessoa();
        if(pessoa==null){
            Status = "Disponível";
        }
        else{
            Status = Umlivro.getPessoa().getNome();
          
        }
           
              model.addRow(new Object[]{Umlivro.getCodigo(),Umlivro.getTitulo(), Umlivro.getAutor(),Status }); 
           
        }
      
    }
}
